home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Acere (PowerPlant, Game) 1.2
/
AcereÄ.sit
/
Acereƒ
/
Code
/
CTextDoc.cp
< prev
next >
Wrap
Text File
|
1995-03-05
|
7KB
|
297 lines
// ===========================================================================
// CTextDoc.cp ⌐1994 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// A simple text document class. It can handle opening, saving, and
// reverting TEXT files. It displays the text in a Window and can
// print the text.
#include "AcereApp.h"
#include "CTextDoc.h"
#include "WellStack.h"
#include "WellFreeCell.h"
#include "WellDeck.h"
#include "CardDeck.h"
#include "CardWindow.h"
#include <LFile.h>
#include <LPrintout.h>
#include <LPlaceHolder.h>
#include <UMemoryMgr.h>
#include <UWindows.h>
#include <String_Utils.h>
#include <LTextEdit.h>
#include <Sound.h>
CTextDoc *theDoc;
extern unsigned long ourAvailRam;
const ResIDT WIND_TextDoc = 500;
const ResIDT prto_TextDoc = 201;
#define kVictorySound 1000
// const OSType Creator_DemoDoc = 'PPd0';
const ResIDT STRx_Untitled = 300;
Boolean hasSmallScreen;
CardWindow *gMainWindow;
// ---------------------------------------------------------------------------
// Ñ CTextDoc(LCommander*, FSSpec*)
// ---------------------------------------------------------------------------
// Construct a TextDoc associated with the specified file
//
// If inFileSpec is nil, then create an empty, untitled document
CTextDoc::CTextDoc(
LCommander *inSuper,
FSSpec *inFileSpec)
: LSingleDoc(inSuper)
{
CardStruct theCardStruct;
// short i;
if (qd.screenBits.bounds.right - qd.screenBits.bounds.left < 640)
hasSmallScreen = true;
else
hasSmallScreen = false;
theDeck = new CardDeck;
currentDeckWell = currentFreeCell = currentStack = 0;
cardRgn = NewRgn();
theDoc = this;
theCardStruct.card = kNoCard;
firstCard = savedFromCard = savedToCard = nil;
// secondCard = nil;
// firstCard.card = secondCard.card = kNoCard;
// Create window for our document
if (hasSmallScreen)
mWindow = CardWindow::CreateWindow(WIND_TextDoc +1, this);
else
mWindow = CardWindow::CreateWindow(WIND_TextDoc, this);
gMainWindow = (CardWindow *)mWindow;
// Specify that the text view should
// be the Target when the Window
// is activated
// mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
// mWindow->SetLatentSub(mTextView);
if (inFileSpec == nil) {
// NameNewDoc(); // Set name of untitled window
} else {
OpenFile(*inFileSpec); // Display contents of file in window
}
}
CTextDoc::~CTextDoc() // destructor
{
delete theDeck;
theDeck = nil;
theDoc = nil;
}
// ---------------------------------------------------------------------------
// Ñ OpenFile
// ---------------------------------------------------------------------------
// Open a new document for the specified File
void
CTextDoc::OpenFile(
FSSpec &inFileSpec)
{
// Create a new File object, read the entire File contents,
// put the contents into the text view, and set the Window
// title to the name of the File.
Try_ {
mFile = new LFile(inFileSpec);
mFile->OpenDataFork(fsRdWrPerm);
Handle textH = mFile->ReadDataFork();
// mTextView->SetTextHandle(textH);
::DisposeHandle(textH);
mWindow->SetDescriptor(inFileSpec.name);
mIsSpecified = true;
}
Catch_(inErr) {
delete this;
Throw_(inErr);
} EndCatch_
}
// ---------------------------------------------------------------------------
// Ñ IsModified
// ---------------------------------------------------------------------------
// Return whether the Document is has changed since the last save
Boolean
CTextDoc::IsModified()
{
// Document has changed if the text view is dirty
// mIsModified = mTextView->IsDirty();
return mIsModified;
}
// ---------------------------------------------------------------------------
// Ñ DoAESave
// ---------------------------------------------------------------------------
// Save Document in the specified file with the specified file type
//
// If file type is fileType_Default, use the normal file type for
// this document
void
CTextDoc::DoAESave(
FSSpec &inFileSpec,
OSType inFileType)
{
delete mFile; // Kill existing file
mFile = new LFile(inFileSpec); // Make new file object
OSType fileType = GameFileType; // Find proper file type
if (inFileType != fileType_Default)
{
fileType = inFileType;
}
// Make new file on disk
mFile->CreateNewDataFile(OurCreator, GameFileType, 0);
mFile->OpenDataFork(fsRdWrPerm);
DoSave(); // Write out data
// Change window name
mWindow->SetDescriptor(inFileSpec.name);
}
// ---------------------------------------------------------------------------
// Ñ DoSave
// ---------------------------------------------------------------------------
// Save the entire Document to its associated File (which must already exist)
void
CTextDoc::DoSave()
{
// Get text and write to file
// Handle textH = mTextView->GetTextHandle();
// StHandleLocker theLock(textH);
// mFile->WriteDataFork(*textH, GetHandleSize(textH));
// mTextView->SetDirty(false); // Saving makes doc un-dirty
}
// ---------------------------------------------------------------------------
// Ñ DoRevert
// ---------------------------------------------------------------------------
// Revert the Document to the last saved version on disk
void
CTextDoc::DoRevert()
{
// Handle textH = mFile->ReadDataFork();
// mTextView->SetTextHandle(textH);
// ::DisposeHandle(textH);
// mTextView->Refresh();
}
// ---------------------------------------------------------------------------
// Ñ DoPrint
// ---------------------------------------------------------------------------
// Print the contents of the Document
void
CTextDoc::DoPrint()
{
LPrintout *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
LPlaceHolder *textPlace = (LPlaceHolder*)
thePrintout->FindPaneByID('TBox');
// textPlace->InstallOccupant(mTextView, atNone);
thePrintout->DoPrintJob();
delete thePrintout;
}
void CTextDoc::CheckVictory(void)
{
short i;
SndListHandle mySndHandle; // handle to an 'snd ' resource
SndChannelPtr mySndChan; // pointer to a sound channel
OSErr myErr;
for (i= 0; i < 4; i++)
{
if (theDeckWells[i]->itsCard->card != 13) // it's not a king
return;
}
mySndChan = nil; // Initialize channel pointer for error checking
// Read in 'snd' resource from resource file
mySndHandle = (SndListHandle) GetResource ('snd ', kVictorySound);
// check for a nil handle
if ( mySndHandle != nil )
{
myErr = SndPlay (mySndChan, mySndHandle, true);
}
}
void CTextDoc::StartNewGame(Boolean isFirstTime)
{
short i;
// GrafPtr thePort = mWindow->GetMacPort();
// SetPort(thePort);
theDeck->ZapOldDeck();
theDeck->GenerateNewDeck(false);
theDoc->currentDeckWell = theDoc->currentFreeCell = theDoc->currentStack = 0;
for (i= 0; i < 4; i++)
{
theDeckWells[i]->itsCard = nil;
// theDeckWells[i]->Draw(nil);
theFreeCells[i]->itsCard = nil;
// theFreeCells[i]->Draw(nil);
theStacks[i]->itsCard = nil;
theStacks[i+4]->itsCard = nil;
}
mWindow->Refresh();
mWindow->UpdatePort();
for (i=0; i < 8; i++)
{
theStacks[i]->InitCards();
if (!isFirstTime)
theStacks[i]->Draw(0L);
}
}